package com.zzkj.util
{
	import com.zzkj.view.form.NumberField;
	
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.utils.describeType;
	
	import flexlib.containers.FlowBox;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Form;
	import mx.containers.FormItem;
	import mx.controls.CheckBox;
	import mx.controls.ComboBox;
	import mx.controls.DateField;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.core.UIComponent;
	
	public class FormUtil
	{
		public function FormUtil()
		{
		}
		//重置Form
		public static function resetForm(forms:Array,vo:Class):void{
			var pxml:XML = describeType(vo) as XML;
			var xmllist:XMLList = pxml..accessor.(@access != "writeonly")+pxml..variable;
			
			for(var i:int=0;i<forms.length;i++){
				var o:Object = forms[i];
				if(!(o is Form)){
					return ;
				}
				var f:Form = forms[i] as Form
				var ch:Array = f.getChildren();
				for(var m:int=0;m<ch.length;m++){
					var obj:Object;
					var fitem:Object = ch[m] as Object;
					if(fitem is FormItem){
							for(var j:uint=0;j<xmllist.length();j++){
								var property:String = xmllist[j].@name;
								obj = fitem.getChildByName(property);
								if(obj!=null){
									 if(obj is NumberField){
										obj.text = '0';
									}else if(obj is TextInput || obj is TextArea){
										obj.text="";
									}else if(obj is ComboBox){
										ComboBox(obj).selectedIndex =-1;
									}else if(obj is CheckBox){
										CheckBox(obj).selected = false;
										CheckBox(obj).dispatchEvent(new Event(Event.CHANGE));
									}else if (obj is DateField){
										DateField(obj).text = '';
										DateField(obj).selectedIndex = -1;
									}
								}
							}			
					}
				}
				
			}
		}
		//从form中获取值
		public static function getValuseInFlowBox(vo:Class,fbox:FlowBox,obj:Object):Object{
			//var obj:Object = new vo();
			var ay:Array = fbox.getChildren();
			for(var i:Number = 0;i<ay.length;i++){
				if(ay[i] is Form){
					getValuesInForm(ay[i],obj);
				}
			}
			//obj.createdDate = new Date();
       		obj.updatedDate = new Date();
			return obj;
		}
		private static function getValuesInForm(f:Form,obj:Object):void{
			var ay:Array = f.getChildren();
			for(var i:Number =0;i<ay.length;i++){
				if(ay[i] is FormItem){
					var item:FormItem = ay[i] as FormItem;
					for(var j:Number = 0;j<item.getChildren().length;j++){
						var control:UIComponent = item.getChildren()[j] as UIComponent;
						if(control is TextInput || control is TextArea){
							if(control.id=="rePassword")
								return;
							obj[control.id] = TextInput(control).text;
						}else if(control is DateField){
							obj[control.id] = DateField(control).selectedDate;
						}else if(control is ComboBox){
	//						var cm:ComboBox = control as ComboBox;
	//						if(cm.selectedItem!=null){
	//							if(obj[cm.id] is Object && !(obj[cm.id] is String))
	//							obj[cm.id] = cm.selectedItem[cm.name];
	//						}
						}else if(control is CheckBox){
							obj[control.id] = CheckBox(control).selected?'是':'否';
						}
					}
					
				}
			}
		}
		/**
		 * ay=fbox.getChildren
		 * entity = VO
		 * **/
		//给comboBox赋值（用于grid的entity传向form时，其中有需要用comboBox显示的时候）
		public static function setComboBoxValue(ay:Array,entity:Object):void{
				for(var i:Number=0;i<ay.length;i++){
					var obj:Object = ay[i] as Object;
					if(obj is Form){
						var fm:Form = Form(obj);
						var ayf:Array = fm.getChildren();
						for(var j:Number=0;j<ayf.length;j++){
							if(ayf[j] is FormItem){
								var fi:FormItem = ayf[j] as FormItem;
								var ch:Array = fi.getChildren();
								for(var m:Number =0;m<ch.length;m++){
									if(ch[m] is ComboBox){
										var c:ComboBox = ch[m] as ComboBox;
										//为false表示不需要setValue
										if(c.name=='false')
											return;
											
										var d:ArrayCollection = c.dataProvider as ArrayCollection;
										if(c.name=="menuItemLabel"){
											trace(d.length);
										}
										
										var index:Number = findValueFormArray(entity[c.id],c.name,d);
										c.selectedIndex = index;
									}
								}
								
							}
						}
					}
				}
				
			}
		private static function findValueFormArray(value:*,property:String,ay:ArrayCollection):Number{
				if(value==null)
					return -1;
				var index:Number = property.indexOf(".");
				for(var i:Number=0;i<ay.length;i++){
					//如果index不等于-1则表示是实体，点后面的则是需要进行比对的属性
					if(index!=-1){
						var pay:Array  = property.split(".");
						//pay[1]取出实体出行
						if(ay[i][pay[1]]===value[pay[1]])
							return i;
					//property不为空，则表示储存的是实体某属性
					}else{
						if(ay[i][property]===value)
							return i;
						else if(ay[i][property] is XMLList && ay[i][property][0]==value){
							return i;
						}
					}
					
					
				}
				return -1;
		}
		
		//为controller的formItem添加 FOCUS_IN和FOCUS_OUT事件
		/**
		 * list=flowBox.getChildren();
		 * */
		public static function addFocusEvent(list:Array):void{
			for(var m:Number=0;m<list.length;m++){
				var ob:Object = list[m];
				if(ob is Form){
					var ay:Array = Form(ob).getChildren();
					for(var i:Number=0;i<ay.length;i++){
						var obj:Object = ay[i] as Object;
						if(obj is FormItem){
							var fi:FormItem = obj as FormItem;
							var ch:Array = fi.getChildren();
							ch[0].addEventListener(FocusEvent.FOCUS_IN,inLabeColor);
							ch[0].addEventListener(FocusEvent.FOCUS_OUT,outLabeColor);
						}
					}
				}
			}
			
		}
			//获取焦点是变成红色
		private static function inLabeColor(e:FocusEvent):void{
				var p:UIComponent = e.currentTarget as UIComponent;
				var fi:FormItem = p.parent as FormItem;
				fi.setStyle('labelStyleName','InlabeFont');
			}
			//失去焦点后变回黑色
			private static function outLabeColor(e:FocusEvent):void{
				var p:UIComponent = e.currentTarget as UIComponent;
				var fi:FormItem = p.parent as FormItem;
				fi.setStyle('labelStyleName','OutlabeFont');
			}
		
		
		
		public static function addChangeEvents(list:Array,
						bindObject:Object):void{
		if(bindObject==null)
			return;
			for(var m:Number=0;m<list.length;m++){
				var ob:Object = list[m];
				if(ob is Form){
					var ay:Array = Form(ob).getChildren();
					for(var i:Number=0;i<ay.length;i++){
						var obj:Object = ay[i] as Object;
						if(obj is FormItem){
							var fi:FormItem = obj as FormItem;
							var ch:Array = fi.getChildren();
							if(ch[0] is TextInput
								|| ch[0] is DateField
								|| ch[0] is TextArea
								|| ch[0] is ComboBox){
								ch[0].addEventListener(Event.CHANGE,changeHandler);
							}
						}
					}
				}
			}
			_bindObject = bindObject;
		}
		
		private static var _bindObject:Object;
		private static var _flag:Boolean ;
		public static function getChangeFlag():Boolean{
			return _flag;
		}
		public static function setFlag(value:Boolean):void{
			_flag = value;
		}
		private static function changeHandler(event:Event):void{
			var Idname:String;
			var f:Boolean = false;
			var target:Object = event.currentTarget;
			if( _bindObject != null){
				if(target is TextInput){
					Idname = TextInput(target).id;
					if(_bindObject[Idname]!=target.text){
						f = true;
					}
				}else if (target is TextArea){
					Idname = TextArea( target).id;
					if(_bindObject[Idname]!=target.text){
						f = true;
					}
					
				}else if(target is ComboBox){
					//TODO---PROBLEM
//					Idname = ComboBox(target).id;
//					var oldc:Object = _bindObject[Idname];
//					var newc:Object = ComboBox(target).selectedItem;
//					var b:Boolean = equals(oldc,newc);
//					if(b==false){
//						f = true;
//					}
					f = true;
				}else if(target is NumberField){
					Idname = NumberField(target).id;
					if(_bindObject[Idname]!=target.text){
						f = true;
					}
				}else if(target is DateField){
					Idname = DateField(target).id;
					var oldd:Date = _bindObject[Idname];
					var newd:Date = DateField(target).selectedDate;
					if(newd != null&&oldd!=null){
						if(oldd.toDateString() != newd.toDateString()){
							f = true;
						} 
					}else if(oldd==null&&newd!=null){
						f = true;
					}
				}
				//valueChangedDic[Idname] = flag;
			}
			setFlag(f);
			//return flag;
		}
		
		
		/***
		 * 比较两个实体
		 * **/
		public static  function equals(obj:Object,target:Object):Boolean{		
			var xml:XML = describeType(obj);
			var properties:XMLList = xml..accessor.(@access != "writeonly")+xml..variable;
			var flag:Boolean = true;
			for(var j:uint=0;j<properties.length();j++){
				var property:String = properties[j].@name;
				if(property!="rowStatus" && property!="recordVersion" 
				&& property!="updatedDate"){
					if(obj[property]!=null && target[property]!=null){
						if(obj[property].toString()!=target[property].toString()){
							flag = false;
							break;
						}
					}else if(obj[property]!=target[property]){
						flag = false;
						break;
					}
				}
			}
			return flag;
		}
		
		public static function isNULLString(s:String):Boolean{
			if(s===null)
				return true;
			else if(s=="")
				return true;
			
			return false;
		}
		public static function isEmpty(obj:Object,pro:String=null):Boolean{
			if(describeType(obj)==null)
				return true;
			if(obj==null)
				return true;
			if(!isNULLString(pro) && isNULLString(obj[pro]))
				return true;
			return false;
		}
			

	}
}